﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Collections.ObjectModel;
using System.Windows;
using SDF.Pagination;

namespace SDF
{
    public class Section: Fragment
    {
        #region Constructor

        public Section()
        {
            Guid = Guid.NewGuid();
        }

        #endregion

        #region Dependency Properties

        public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register(
            "HeaderTemplate",
            typeof(DataTemplate),
            typeof(Section),
            new FrameworkPropertyMetadata(null));

        public static readonly DependencyProperty FooterTemplateProperty = DependencyProperty.Register(
            "FooterTemplate",
            typeof(DataTemplate),
            typeof(Section),
            new FrameworkPropertyMetadata(null));

        public static readonly DependencyProperty FirstPageNumberProperty = DependencyProperty.Register(
            "FirstPageNumber",
            typeof(int),
            typeof(Section),
            new FrameworkPropertyMetadata(1));

        public static readonly DependencyProperty ResetPageNumbersProperty = DependencyProperty.Register(
            "ResetPageNumbers",
            typeof(bool),
            typeof(Section),
            new FrameworkPropertyMetadata(false));

        #endregion

        #region Properties

        public Guid Guid { get; private set; }

        public DataTemplate HeaderTemplate
        {
            get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
            set { SetValue(HeaderTemplateProperty, value); }
        }

        public DataTemplate FooterTemplate
        {
            get { return (DataTemplate)GetValue(FooterTemplateProperty); }
            set { SetValue(FooterTemplateProperty, value); }
        }
        
        public int FirstPageNumber
        {
            get { return (int)GetValue(FirstPageNumberProperty); }
            set { SetValue(FirstPageNumberProperty, value); }
        }

        public bool ResetPageNumbers
        {
            get { return (bool)GetValue(ResetPageNumbersProperty); }
            set { SetValue(ResetPageNumbersProperty, value); }
        }

        #endregion

        #region Overriden Methods

        public override bool IsValidParent(DependencyObject parent)
        {
            if (parent is PageContent)
                return true;

            return false;
        }

        public override SmartBlock GetEmptyCopy()
        {
            var result = new Section();
            CopyProperties(result);
            return result;
        }

        protected override void CopyProperties(SmartTextElement copy)
        {
            base.CopyProperties(copy);

            var result = copy as Section;
            if (result == null)
                throw new ArgumentException("Copy must be a section");

            result.Guid = this.Guid;
            result.HeaderTemplate = this.HeaderTemplate;
            result.FooterTemplate = this.FooterTemplate;
            result.FirstPageNumber = this.FirstPageNumber;
            result.ResetPageNumbers = this.ResetPageNumbers;
        }

        public override HarvestResult Harvest(HarvestingContext context, double remainingHeight, SmartBlock previousBlock)
        {
            // In SDF, only one section is allowed in each page (although a section can span multiple pages)
            if (PageContent.IsDirectChild(this) && (previousBlock != null))
            {
                return HarvestResult.BreakPage;
            } 


            return base.Harvest(context, remainingHeight, previousBlock);
        }

        #endregion
    }
}
